package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseMaterielDetailFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.MaterieListOriginal.MaterieListContent;
import com.isunland.managesystem.entity.OderSaleContent;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.NumberUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

public class ShowMaterieDetailFragment extends BaseMaterielDetailFragment {
  public static final String EXTRA_CONTENT = "com.isunland.managesystem.entity.EXTRA_CONTENT";
  public static final String EXTRA_ORDER_CONTENT =
      "com.isunland.managesystem.entity.EXTRA_ORDER_CONTENT";

  private MaterieListContent mContent;
  private OderSaleContent orderContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    mContent = (MaterieListContent) getArguments().getSerializable(EXTRA_CONTENT);
    orderContent = (OderSaleContent) getArguments().getSerializable(EXTRA_ORDER_CONTENT);
    LogUtil.i("deleted==" + mContent.getId());
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_delete_edit, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        //			if (NavUtils.getParentActivityName(getActivity())!=null) {
        //				NavUtils.navigateUpFromSameTask(getActivity());
        //			}

        getActivity().finish();

        break;
      //编辑
      case R.id.menu_item_alter:
        if ("new".equals(orderContent.getMoutbillStatus()) || "abort".equals(
            orderContent.getMoutbillStatus())) {
          Intent i = new Intent(getActivity(), EditMaterieDetailActivity.class);
          i.putExtra(EditMaterieDetailFragment.EXTRA_CONTENT, mContent);
          i.putExtra(EditMaterieDetailFragment.EXTRA_ORDER_CONTENT, orderContent);
          startActivityForResult(i, DETAIL_MATERIEL);
        } else {
          ToastUtil.show(R.string.draftOrAbort);
        }

        break;
      //删除物料明细
      case R.id.menu_item_delete:
        if ("new".equals(orderContent.getMoutbillStatus()) || "abort".equals(
            orderContent.getMoutbillStatus())) {
          //				volleyPoastDelete();
          showDialog(DELETE_MATERIEL);
        } else {
          ToastUtil.show(R.string.draftOrAbort);
        }

        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  //删除物料明细
  private void volleyPoastDelete() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_MATERIE_SAVE);
    HashMap<String, String> params = new HashMap<>();
    params.put("id", mContent.getId());
    HashMap<String, String> mparams = new HashMap<>();
    mparams.put("deleted", "[" + new JSONObject(params).toString() + "]");
    LogUtil.i("deleted" + params.toString());
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, mparams, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        try {
          LogUtil.i("response=" + arg0);
          MyUtils.dismissDialog();
          SuccessMessage success = new Gson().fromJson(arg0, SuccessMessage.class);
          if (null == success || null == success.getResult()) {
            Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
            return;
          }
          String successType = success.getResult();
          if (successType.equals(SuccessMessage.FAILURE)) {
            Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
            return;
          }
          if (successType.equals(SuccessMessage.SUCCESS)) {
            Toast.makeText(getActivity(), R.string.success_operation, Toast.LENGTH_SHORT).show();

            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
          }
        } catch (JsonSyntaxException e) {
          e.printStackTrace();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
      }
    });
  }

  @Override protected void setTitle() {
    setTitleCustom(R.string.materiel_detail);
  }

  @Override protected void initMaterialData() {
    //物料类别
    tvMaterielCategory.setText(mContent.getMaterialKindName());

    tvMaterielName.setText(mContent.getMaterialName());
    //		tvMaterielCode.setText(mContent.getsInbillCode());
    tvMaterielCode.setText(mContent.getMaterialCode());
    tvMaterielSelfCode.setText(mContent.getMaterialSelfCode());
    LogUtil.i("MaterialSelfcode==" + mContent.getMaterialSelfCode());
    tvMaterielType.setText(mContent.getMtype());//规格型号
    tvBaseApartment.setText(mContent.getMunit());//基本单位
    tvUnitPrice.setText(NumberUtil.numberBigToString(mContent.getMoutPrice()));//单价
    etNumber.setText(NumberUtil.numberBigToString(mContent.getMoutNumber()));//数量
    //总价
    etallPrice.setText(NumberUtil.numberBigToString(mContent.getMoutsumPrice()));
    etNotes.setText(mContent.getRemark());//备注

    etNumber.setEnabled(false);
    etNotes.setEnabled(false);
    tvUnitPrice.setEnabled(false);
    ibMaterielName.setVisibility(View.GONE);
    ibMaterielCategory.setVisibility(View.GONE);

    //		llmaterialGone.setVisibility(View.VISIBLE);
    //		tvbatchstorage.setText(mContent.getSinbillBatch());//入库批次
    //		tvProductiondate.setText(mContent.getSmproddate());//生产日期
    //		tvValidityperiod.setText(mContent.getSmexpDate());//有效期

  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == DETAIL_MATERIEL) {
      getActivity().setResult(Activity.RESULT_OK);
      getActivity().finish();
    }
    if (requestCode == DELETE_MATERIEL) {
      volleyPoastDelete();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static ShowMaterieDetailFragment newInstance(MaterieListContent item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, item);
    ShowMaterieDetailFragment fragment = new ShowMaterieDetailFragment();
    fragment.setArguments(args);
    return fragment;
  }

  public static ShowMaterieDetailFragment newInstance(MaterieListContent item,
      OderSaleContent orderContent) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, item);
    args.putSerializable(EXTRA_ORDER_CONTENT, orderContent);
    ShowMaterieDetailFragment fragment = new ShowMaterieDetailFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
